package org.jeecg.modules.yw.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.modules.yw.entity.Announcement;
import org.jeecg.modules.yw.result.Result;
import org.jeecg.modules.yw.service.AnnouncementService;
import org.jeecg.modules.yw.utils.CommonUtils;
import org.jeecg.modules.yw.utils.DateUtils;
import org.jeecg.modules.yw.utils.UserUtil;
import org.jeecg.modules.yw.vo.AnnouncementVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Api(tags = "公告管理")
@RequestMapping("/app/announcements")
public class AnnouncementController {
    @Autowired
    private AnnouncementService announcementService;
    @Autowired
    private ISysBaseAPI sysBaseAPI;
    @ApiOperation(value = "获取公告列表")
    @GetMapping("/page")
    public  org.jeecg.common.api.vo.Result page(Page<Announcement> page,@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize ) {
        page.setCurrent(pageNo);
        page.setSize(pageSize);
        IPage<AnnouncementVo> result = announcementService.pageInfo(page);
        return  org.jeecg.common.api.vo.Result.ok(result);
    }
    @ApiOperation(value = "通过角色获取公告列表")
    @GetMapping("/pageRoleid")
    public Result pageRoleid(int roleid,HttpServletRequest request) throws Exception {
        Page<Announcement> page=new Page<>(1,3);
        IPage<Announcement> result = announcementService.pageInfoByRoleId(roleid,page,request);
        return Result.success(result);
    }
    @ApiOperation(value = "添加公告")
    @PostMapping("/addAnnouncement")
    @Transactional
    public Result addAnnouncement(@RequestBody AnnouncementVo announcementVo, HttpServletRequest request) throws Exception {
        Announcement announcement = new Announcement();
        announcement.setId(CommonUtils.getID());
        BeanUtils.copyProperties(announcementVo, announcement);
        announcement.setCreator(UserUtil.getUserId(request,sysBaseAPI));
        announcement.setCreatetime(DateUtils.getCurrentDate());
        boolean re = announcementService.save(announcement);
        if (!re) {
            return Result.error("添加公告失败");
        }
        if (announcementVo.getScope() != null) {
            announcementService.saveBatchAnnouncementScope(announcementVo.getScope(), announcement.getId());
        }
        return Result.success("添加公告成功");
    }
    @ApiOperation(value = "修改公告")
    @PostMapping("/editAnnouncement")
    @Transactional
    public Result editAnnouncement(@RequestBody AnnouncementVo announcementVo) {
        Announcement announcement = new Announcement();
        BeanUtils.copyProperties(announcementVo, announcement);
        boolean re= announcementService.updateById(announcement);
        if (!re) {
            return Result.error("修改公告失败");
        }
        if (announcementVo.getScope() != null) {
            announcementService.deleteAnnouncementScope(announcement.getId());
            announcementService.saveBatchAnnouncementScope(announcementVo.getScope(), announcement.getId());
        }
        return Result.success("修改公告成功");
    }
    @ApiOperation(value = "删除公告")
    @DeleteMapping("/deleteAnnouncement")
    @Transactional
    public org.jeecg.common.api.vo.Result deleteAnnouncement(String id) {
        boolean re = announcementService.removeB(id);
        if (!re) {
            return org.jeecg.common.api.vo.Result.error("删除公告失败");
        }
        return org.jeecg.common.api.vo.Result.ok("删除公告成功");
    }

}
